//
//  xsChartletCollectionView.m
//  digital_robot_demo
//
//  Created by xskj on 2024/8/30.
//

#import "xsChartletCollectionView.h"
  
@interface xsChartletCollectionView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong)NSArray *dataArray;

@end

@implementation xsChartletCollectionView


- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout withData:(NSArray *)dArray{
    
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if(self){
        self.delegate = self;
        self.dataSource = self;
        
        self.dataArray = dArray;
        self.backgroundColor = UIColor.clearColor;//COLOR_VCViewBgcolor;
        
        //        //注册cell
        [self registerClass:[xsChartletCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([xsChartletCollectionViewCell class])];
        [self registerClass:[xsChartletCollectionViewHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([xsChartletCollectionViewHeadView class])];
    }
    
    return self;
}

#pragma mark - UICollectionViewDataSource
//一共有多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

//每一组有多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

//每一个cell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    xsChartletCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([xsChartletCollectionViewCell class]) forIndexPath:indexPath];
    
    if (indexPath.row<self.dataArray.count) {
        cell.model = self.dataArray[indexPath.item];
    }
    cell.backgroundColor = UIColor.clearColor;
    cell.contentView.backgroundColor = UIColor.clearColor;
    cell.layer.cornerRadius = 6.0;
    cell.layer.masksToBounds = YES;
    
    return cell;
}

//和tableView差不多, 可设置头部和尾部
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        xsChartletCollectionViewHeadView *headerView = [self dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([xsChartletCollectionViewHeadView class]) forIndexPath:indexPath];
        headerView.backgroundColor = UIColor.clearColor;
        return headerView;
    }
//    else if([kind isEqualToString:UICollectionElementKindSectionFooter])
//    {
//        
//    }
    return nil;
}


//每一个分组的上左下右间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0);
}

//头部视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//    return CGSizeMake(self.frame.size.width, 45.0);
    return CGSizeMake(self.frame.size.width, 0.10);
}


//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat margin = 5.0*5.0;
    CGFloat itemW =  (self.frame.size.width-margin)*0.25;
    CGFloat itemH = itemW;//*(4.0/3.0);
    return CGSizeMake(itemW, itemH);
}

//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//    return 4;
//}
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//    return 5;
//}

//cell的点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.item<self.dataArray.count){
        xsChartletCollectionViewCellModel *model = self.dataArray[indexPath.item];
        
        NSLog(@"model.iconUrlString==%@\n",model.iconUrlString);
        
    }
}

//
////在开始移动是调动此代理方法
//- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"开始移动");
//    return YES;
//}

////在移动结束的时候调用此代理方法
//- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
//    NSLog(@"结束移动");
//
//}


@end



//UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
//// 3. 横向间距
//la.minimumInteritemSpacing = 5.0;
//// 4. 纵向间距
//la.minimumLineSpacing = 5.0;
//NSMutableArray *tempMarr = [NSMutableArray new];
//for (int i =0; i<10; i++) {
//    xsChartletCollectionViewCellModel *md = [xsChartletCollectionViewCellModel new];
//    md.title = [NSString stringWithFormat:@"titile--%d",i];
//    [tempMarr addObject:md];
//}
//
//dispatch_async(dispatch_get_main_queue(), ^{
//    XSQuotesThemeItemCollectionView *vv = [[XSQuotesThemeItemCollectionView alloc]initWithFrame:CGRectMake(10.0, 200.0, kScreenW-10.0, 200.0) collectionViewLayout:la];
//    vv.backgroundColor = COLOR_HEX(0xF4F6F7);
//    vv.layer.cornerRadius = 6.0;
//    vv.layer.masksToBounds = YES;
//    [weakSelf.view addSubview:vv];
//    vv.dataArray = tempMarr;
//    [vv reloadData];
//});

//
//  XSQuotesThemeItemCollectionView.h
//  CrewordForiOS
//
//  Created by mac on 5/23/22.
//  Copyright © 2022 WenYuKeJi. All rights reserved.
//
//
//
//UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
//// 3. 横向间距
//la.minimumInteritemSpacing = 5.0;
//// 4. 纵向间距
//la.minimumLineSpacing = 5.0;
//
//
//XSQuotesThemeItemCollectionView *collectionV = [[XSQuotesThemeItemCollectionView alloc]initWithFrame:CGRectMake(10.0, 200.0, kScreenW-20.0, 0.0) collectionViewLayout:la];
//collectionV.backgroundColor = COLOR_HEX(0xF4F6F7);
//collectionV.layer.cornerRadius = 6.0;
//collectionV.layer.masksToBounds = YES;
//[self.view addSubview:collectionV];
//self.collectionV = collectionV;
////    collectionV.dataArray = tempMarr;
////    [collectionV reloadData];
//XSWeakSelf;
//collectionV.collectionViewCellBeclick = ^(XSTagCollectionViewCellModel * _Nonnull model) {
//if(notNillStr(model.conName)){
//    weakSelf.selectThemeItemLabel.text = model.conName;
//}
//dispatch_async(dispatch_get_main_queue(), ^{
//    [weakSelf updateViewFrameWith:NO];
//});
//weakSelf.libraryId = model.Id;
//[weakSelf.tableView.mj_header beginRefreshing];
//};
//
//[collectionV mas_makeConstraints:^(MASConstraintMaker *make) {
//make.left.equalTo(self.view.mas_left).offset(10.0);
//make.right.equalTo(self.view.mas_right).offset(-10.0);
//make.top.equalTo(self.themeBgView.mas_bottom);
//make.height.mas_equalTo(0.0);
//}];
//collectionV.hidden = YES;







